﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using AgnisEbmtTranslator;
using System.Data.SqlClient;

namespace TestApplication
{
    public partial class FormTest2 : Form
    {
        public FormTest2(String ebmtConnectionString, String mappingInfoConnectionString, String AgnisStagingConnectionString)
        {
            InitializeComponent();

            // initialize events
            m_EventStopThread = new ManualResetEvent(false);
            m_EventThreadStopped = new ManualResetEvent(false);

            currentEbmtDbConnectionString = ebmtConnectionString;
            currentMappingInfoConnectionString = mappingInfoConnectionString;
            currentAgnisStagingConnectionString = AgnisStagingConnectionString;
        }

        #region
        CGlobalProcess oP0;
        private int nTransplants = 1;
        private int selectedIndex;
        // events used to stop worker thread
        ManualResetEvent m_EventStopThread;
        ManualResetEvent m_EventThreadStopped;

        private String currentEbmtDbConnectionString;
        private String currentMappingInfoConnectionString;
        private String currentAgnisStagingConnectionString;
       

        #endregion
       
      
        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; }
        }

        private void btnTest0_Click(object sender, EventArgs e)
        {
            if (StartGlobalProcess())
            {
                txtForm2.Text += "=> P0 successfull";
            }
            else
            {
                txtForm2.Text += "=> P0 in failure";
            }

            
        }

        private bool StartGlobalProcess()
        {            
            if(bsTransplantsList.Count > 0)
            {

                for (int i = 0; i < bsTransplantsList.Count; i++)
                {
                    DataRowView dView;
                    selectedIndex = bsTransplantsList.IndexOf(this.bsTransplantsList.Current);
                    if (nTransplants == 1 && selectedIndex >= 0 && selectedIndex < bsTransplantsList.Count)
                    {
                        dView = (DataRowView)bsTransplantsList[selectedIndex];
                    }
                    else
                    {
                        dView = (DataRowView)bsTransplantsList[i];
                    }
                    

                    oP0 = new CGlobalProcess();
                    oP0.SetCurrentEbmtDbConnectionString(currentEbmtDbConnectionString);
                    oP0.SetCurrentMappingInfoConnectionString(currentMappingInfoConnectionString);
                    oP0.SetCurrentAgnisStagingConnectionString(currentAgnisStagingConnectionString);
                    oP0.process0(Convert.ToInt32(dView["ID"]), Convert.ToInt32(dView["IDAA"]), (DateTime)dView["IDAABC"], DateTime.Today.Date, Convert.ToDateTime("12/12/2999"));
                    txtForm2.Text = oP0.sTask2Result + "\r\n";                    
                }
                return true;
            }
            else
            {
                return false;
            }

        }

        private void FormTest2_Load(object sender, EventArgs e)
        {
            Load_Form();
        }

        public void Load_Form()
        {
            try
            {
                SqlConnection myTestConnection = new SqlConnection(currentEbmtDbConnectionString);
                this.transplantsListTableAdapter1.SetConnection(myTestConnection);
                this.transplantsListTableAdapter1.Fill(this.ebmtDataSet.TransplantsList);
            }
            catch (Exception ex)
            {
                txtForm2.Text = ex.ToString();
            }
        }

        private void btnTest1_Click(object sender, EventArgs e)
        {

        }

    }
}
